﻿using AutoMapper;
using HomeCloudStorageSystem.Dtos;
using HomeCloudStorageSystem.Models;
using HomeCloudStorageSystem.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.IdentityModel.Tokens;
using System.Security.Claims;

namespace HomeCloudStorageSystem.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class DiarysController : ControllerBase
    {
        private readonly IDiaryRepository _diaryRepository;
        private readonly IUserRepository _userRepository;
        private readonly IMapper _mapper;
        public DiarysController(IDiaryRepository diaryRepository,IMapper mapper, IUserRepository userRepository)
        {
            _diaryRepository = diaryRepository;
            _mapper = mapper;
            _userRepository = userRepository;
        }
        /// <summary>
        /// 根据用户ID查看日记信息列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>日记列表</returns>
        [HttpGet]
        public IActionResult GetDiaryByUserIds()
        {
            var userName = User.Claims.FirstOrDefault(n => n.Type == ClaimTypes.NameIdentifier)?.Value;
            var userData = _userRepository.GetUserByUserName(userName);
            var diaryFromRepositorys = _diaryRepository.GetDiarieByUserIds(userData.Id);
            if(diaryFromRepositorys == null)
            {
                return NotFound();
            }
            return Ok(diaryFromRepositorys);
        }
        /// <summary>
        /// 根据用户ID和日记ID查询日记信息
        /// </summary>
        /// <param name="id">日记ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns>日记信息</returns>
        //[HttpGet("{diaryId}")]
        //public IActionResult GetDiaryByIdUserId([FromRoute] int diaryId)
        //{
        //    if (string.IsNullOrEmpty(diaryId.ToString()))
        //    {
        //        return BadRequest("ID不能为空");
        //    }
        //    var userName = User.Claims.FirstOrDefault(n => n.Type == ClaimTypes.NameIdentifier)?.Value;
        //    var userData = _userRepository.GetUserByUserName(userName);

        //    var diaryFromRepository = _diaryRepository.GetDiaryByIdUserId(diaryId, userData.Id);
        //    if (diaryFromRepository == null)
        //    {
        //        return NotFound();
        //    }
        //    return Ok(diaryFromRepository);

        //}
        /// <summary>
        /// 新增日记信息接口
        /// </summary>
        /// <param name="diaryForCreationDto">日记信息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateDiary([FromBody] DiaryForCreationDto diaryForCreationDto)
        {
            if(diaryForCreationDto == null)
            {
                return BadRequest();
            }
            var userName = User.Claims.FirstOrDefault(n => n.Type == ClaimTypes.NameIdentifier)?.Value;
            var userData = _userRepository.GetUserByUserName(userName);
            diaryForCreationDto.UserId = userData.Id;
            var diaryModle = _mapper.Map<Diary>(diaryForCreationDto);
            _diaryRepository.AddDiary(diaryModle);
            _diaryRepository.Save();
            return Ok("新增成功");
        }
        /// <summary>
        /// 删除日记信息接口
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="id">日记ID</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteDiary()
        {
            var strList = HttpContext.Request.Path.ToString().Split('/');
            var id = Convert.ToInt32(strList.Last());
            var diaryFromRepository = _diaryRepository.GetDiaryByIdUserId(id);
            if (diaryFromRepository == null)
            {
                return NotFound();
            }
            _diaryRepository.RemoveDiary(diaryFromRepository);
            _diaryRepository.Save();
            return Ok();
        }
    }
}
